package icu.bughub.app.app.viewmodel

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import icu.bughub.app.app.R
import icu.bughub.app.app.model.entity.Category
import icu.bughub.app.app.model.entity.DataType
import icu.bughub.app.app.model.entity.SwiperEntity
import icu.bughub.app.app.model.service.HomeService

class MainViewModel : ViewModel() {

    val homeService = HomeService.instance()

    //分类数据是否加载成功
    var categoryLoadSuccess by mutableStateOf(false)
        private set

    //分类数据
    var categories by mutableStateOf(
        listOf(
            Category(title = "思想政治1"),
            Category(title = "法律法规2"),
            Category(title = "职业道德3"),
            Category(title = "诚信自律4")
        )
    )
        private set


    suspend fun getCategoryList() {
        val categoryRes = homeService.categoryList()
        if (categoryRes.code == 0) {
            categories = categoryRes.data
            categoryLoadSuccess = true
        } else {
            //不成功的情况= 读取message
            val message = categoryRes.message
        }

    }


    //当前分类选中
    var categoryIndex by mutableStateOf(0)
        private set

    //更新分类下标选中
    fun updateCategoryIndex(index: Int) {
        categoryIndex = index
    }


    //类型数据
    val types by mutableStateOf(
        listOf(
            DataType(title = "相关咨询", iconId = R.drawable.ic_tab_news),
            DataType(title = "视频课程", iconId = R.drawable.ic_tab_video),
        )


    )

    //当前类型选中下标
    var currentTypeIndex by mutableStateOf(0)
        private set


    //是否显文章列表
    var showArticleList by mutableStateOf(true)
        private set

    fun updateTypeIndex(index: Int) {
        currentTypeIndex = index
        showArticleList = currentTypeIndex == 0
    }


    //轮播图数据
    var swiperData by mutableStateOf(
        listOf(
            SwiperEntity(
                title = "雷佳音《长安的荔枝》今晚开播！天崩开局，一文get关键剧情点",
                imageUrl = "https://inews.gtimg.com/om_ls/Ow2lku6kEUs9xAgqg9bJS8Kaz0JhyrGT_1m80oZEeuzwAAA_870492/0",
                description = "雷佳音《长安的荔枝》今晚开播！天崩开局，一文get关键剧情点",
            ),
            SwiperEntity(
                title = "陈德容首谈离婚太洒脱：中年失婚的港台童年女神们，各有各的辛酸",
                imageUrl = "https://inews.gtimg.com/news_ls/O_BcvMewCePreBWZinVCjNtitDLVYQ3quG3AmNE5ohB4AAA_870492/0",
                description = "陈德容首谈离婚太洒脱：中年失婚的港台童年女神们，各有各的辛酸",
            ),
            SwiperEntity(
                title = "华语乐坛，越来越招笑了哈",
                imageUrl = "https://inews.gtimg.com/om_ls/O55sLwkLEjsmlaZgg_h_O8FsoDbYJD4w3uGyr5heujkacAA_870492/0",
                description = "华语乐坛，越来越招笑了哈",
            ),
            SwiperEntity(
                title = "对华新招？美国被曝暂停向中国出售核电站设备",
                imageUrl = "https://inews.gtimg.com/news_ls/OtHeqyX7FjqAsBm3h82vSINzO8h162qoVDsQIQUJS64rgAA_870492/0",
                description = "刘宇宁和他的粉丝，真正的内娱“双强”",
            ),
        )

    )
        private set

    var bannerLoadSuccess by mutableStateOf(false)
        private set

    suspend fun getBannerList() {
        val bannerRes = homeService.getSwiperData()
        if (bannerRes.code == 0) {
            swiperData = bannerRes.data
            bannerLoadSuccess = true
        } else {
            //不成功的情况= 读取message
            val message = bannerRes.message
        }
    }


    //通知数据
    val notifications = listOf(
        "华语乐坛，越来越招笑了哈",
        "《长安的荔枝》今晚开播！天崩开局，一文get关键剧情点",
        "陈德容首谈离婚太洒脱：中年失婚的港台童年女神们，各有各的辛酸",
        "对华新招？美国被曝暂停向中国出售核电站设备",
    )


}